import { getQueryString } from 'nearby-common'
import { message } from 'ant-design-vue'
import { mscf } from './const'
import Vue from 'vue'

export function getToken() {
  return getQueryString('token') || 'eyJhbGciOiJIUzI1NiJ9.eyJ1c2VyX2Jhc2VfaWQiOiIyNTk0MHwxNjYzMjIyMTAyOTA3In0.cwC_8og5YWGzbZgrfD5MxL35EPXa7Hg6Z4imtquHW_I'
}

function isInFrame() {
  return window.self !== window.top
}

const mcMessage = function() {}

mcMessage.error = function(content) {
  if (isInFrame()) {
    mscf.emit('toast.error', '*', content)
  } else {
    message.error(content)
  }
}
mcMessage.success = function(content) {
  if (isInFrame()) {
    mscf.emit('toast.success', '*', content)
  } else {
    message.success(content)
  }
}
mcMessage.warning = function(content) {
  if (isInFrame()) {
    mscf.emit('toast.warning', '*', content)
  } else {
    message.warning(content)
  }
}
export const messageInstance = mcMessage

// 接受文件流并下载
export function getExcel(name, blobObj) {
  var blob = new Blob([blobObj])
  var elink = document.createElement('a')
  elink.download = name
  elink.style.display = 'none'
  elink.href = URL.createObjectURL(blob)
  document.body.appendChild(elink)
  elink.click()
  document.body.removeChild(elink)
  URL.revokeObjectURL(blob)
}
Vue.filter('handleData', (val) => {
  if (!val && val !== 0) return '无'
  return val
})
Vue.filter('filterSpuStatus', (text) => {
  if (text === 0) {
    return '审核中'
  } else if (text === 1) {
    return '上架'
  } else if (text === 2) {
    return '下架'
  } else if (text === 3) {
    return '审核拒绝'
  } else if (text === 4) {
    return '待重新同步'
  } else if (text === 5) {
    return '请等待系统自动同步'
  } else if (text === 6) {
    return '不具备提审条件'
  } else {
    return '未知'
  }
})
